package cn.toma.core.web;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * @author zqx
 * @version 1.0.0
 * @ClassName JWTUtil.java
 * @Description
 * @createTime 2019年09月02日 09:26:00
 */
@Slf4j
public class JWTUtil
{
	/**
	 * 一天
	 */
	private static final long EXPIRE_TIME = 86400L * 1000;

	/**
	 * 校验 token是否正确
	 *
	 * @param token  密钥
	 * @param secret 用户的密码
	 * @return 是否正确
	 */
	public static boolean verify(String token, String username, String secret)
	{
		try {
			Algorithm algorithm = Algorithm.HMAC256(secret);
			JWTVerifier verifier = JWT.require(algorithm)
					.withClaim("username", username)
					.build();
			verifier.verify(token);
			log.info("token is valid");
			return true;
		} catch (Exception e) {
			log.info("token is invalid{}", e.getMessage());
			return false;
		}
	}

	/**
	 * 从 token中获取用户名
	 *
	 * @return token中包含的用户名
	 */
	public static String getUsername(String token)
	{
		try {
			DecodedJWT jwt = JWT.decode(token);
			return jwt.getClaim("username").asString();
		} catch (JWTDecodeException e) {
			log.error("error：{}", e.getMessage());
			return null;
		}
	}

	/**
	 * 生成 token
	 *
	 * @param username 用户名
	 * @param secret   用户的密码
	 * @return token
	 */
	public static String sign(String username, String secret)
	{
		try {
//			username = StringUtils.lowerCase(username);
			Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
			Algorithm algorithm = Algorithm.HMAC256(secret);
			return JWT.create()
					.withClaim("username", username)
					.withExpiresAt(date)
					.sign(algorithm);
		} catch (Exception e) {
			log.error("error：{}", e);
			return null;
		}
	}
}
